package com.cwh.realm;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cwh.entity.Employee;
import com.cwh.entity.EmployeeExample;
import com.cwh.entity.EmployeeExample.Criteria;
import com.cwh.mapper.EmployeeMapper;
import com.cwh.service.EmployeeService;
import com.sun.javafx.logging.Logger;

public class CustomRealm1 extends AuthorizingRealm{
	
	
	@Override
	public void setName(String name) {
		// TODO 自动生成的方法存根
		super.setName("reaml名称");
	}
	
	@Autowired
	private EmployeeService employeeService;
	
	//认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取身份信息的用户名
		String username=(String)token.getPrincipal();
		String password=(String)token.getCredentials();
		System.out.println(username);
		System.out.println(password);
		Employee employee = employeeService.findEmployeeByName(username);
		//查询失败
		if(employee==null) {
			return null;
		}
		
		//将用户输入的数据md5(加盐)
		SimpleAuthenticationInfo simpleAuthenticationInfo 
		= new SimpleAuthenticationInfo(
				employee,employee.getPassword(),ByteSource.Util.bytes(employee.getSalt()),this.getName());
//		
		return simpleAuthenticationInfo;

	}
	
	//授权 先不做
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		return null;
	}
	

}
